package api

import (
	"Management/dao"
	"github.com/gin-gonic/gin"
)

//查询所有部门
func QueryAllDepartment(c  * gin.Context){
	departments:=dao.QueryDepartments()
	c.JSON(200,gin.H{"departments":departments})
}
//添加部门
func AddDepartment(c *gin.Context){
	type Msg struct {
		IdAgo int `json:"id_ago"`
		DepartmentName string  `json:"department_name"`
		Responsibility string `json:"responsibility"`
		Phone string `json:"phone"`
	}
	var msg Msg
	err:=c.BindJSON(&msg)
	if err!=nil{
		panic(err)
	}
	department1:=dao.QueryDepartment(msg.IdAgo)
	err=dao.AddDepartment(msg.IdAgo,department1.Path,msg.DepartmentName,msg.Responsibility,msg.Phone)
	if err!=nil {
		c.JSON(400,gin.H{"msg":"添加部门存入数据库错误","err":err})
		return
	}
	c.JSON(200,gin.H{"msg":"添加部门成功","do":1})
}
//删除部门
func DeleteDepartment(c *gin.Context){
	type Msg struct {
		Id int `json:"id"`
	}
	var msg Msg
	err:=c.BindJSON(&msg)
	if err!=nil{
		panic(err)
	}
	err=dao.DeleteDepartment(msg.Id)
	if err!=nil{
		panic(err)
	}
	c.JSON(200,gin.H{"msg":"删除部门成功","do":1})
}
//修改部门
func ChangeDepartment(c *gin.Context){
	type Msg struct {
		Id int `json:"id"`
		DepartmentName string `json:"department_name"`
		Responsibility string  `json:"responsibility"`
		Phone string `json:"phone"`
	}
	var msg Msg
	err:=c.BindJSON(&msg)
	if err!=nil{
		panic(err)
	}
	err=dao.ChangeDepartment(msg.Id,msg.DepartmentName,msg.Responsibility,msg.Phone)
	if err!=nil{
		panic(err)
	}
	c.JSON(200,gin.H{"msg":"修改部门成功","do":1})
}
